

#define all the data structure for tianya forum

from google.appengine.ext import db
from xmw.tianya.error_tianya import *
import uuid
class Thread(db.Model):
    title=db.StringProperty(required=True)
    
    #uuid
    thread_id=db.StringProperty(multiline=False, required=True)
    
    #changed on thread change
    last_change=db.DateTimeProperty()
    
    created=db.DateTimeProperty(auto_now_add=True)
    
    status=db.StringProperty()
    
    page_url=db.StringListProperty()
    
    
class Page(db.Model):
    page_id=db.StringProperty(multiline=False,required=True) #uuid
    seq_in_thread=db.IntegerProperty(required=True) #�ڼ�ҳ
    url=db.StringProperty(required=True)
    created=db.DateTimeProperty(auto_now_add=True)
    last_scan=db.DateTimeProperty(required=True)
    latest_post_date=db.DateTimeProperty()
    latest_status=db.StringProperty()      #��ǰҳ���״̬��ʹ��http״̬�룬200/404��
    
    #thread_id=db.StringProperty()   #thread current page belongs to
    

class User(db.Model):
    user_id=db.StringProperty(multiline=False,required=True)    #uuid
    mail_box=db.EmailProperty(required=True)
    join_date=db.DateTimeProperty(required=True,auto_now_add=True)

class Thread_User_Map(db.Model):
    seq =db.IntegerProperty(required=True)
    thread_id   =db.StringProperty(multiline=False,required=True)
    user_id =db.StringProperty(multiline=False,required=True)
    created=db.DateTimeProperty(required=True,auto_now_add=True)
    
class SubReqest(db.Model):
    mail_box = db.EmailProperty(required=True)
    url =   db.StringProperty(multiline=False,required=True)
    is_binded                 =               db.BooleanProperty()
    is_threaded =   db.BooleanProperty()

    
"""
    def __init__(self,mail_box,url,is_binded=False,is_threaded=False):
        db.Model.__init__(self,mail_box,url,is_binded,is_threaded)
        self.mail_box   =mail_box
        self.url    =url
        self.is_binded =is_binded
        self.is_threaded=is_threaded
"""    

        
"""
#key as locks for thread changes
tianya_thread_lock_key_name="tianya_thread_lock_key"
class ThreadLock():
    def acquire(self):
        pass
    
    def release(self):
        pass
    


def insert_thread(thread):
    #tianya_thread_key_name
    first_page_url=thread.page_url[0]
    results = db.GqlQuery("SELECT * FROM Thread WHERE page_url = %s"%first_page_url)
    same_thread=result.get()
    
    #if found,means there is already a thread
    if same_thread not None:
        e=THREAD_EXIST("thread existed when checking thread with page url:%s"first_page_url)
        e.log()
        raise e
    
    #取得Thread 锁，插表
    
    

    pass
"""    
def add_thread(thread):
    if thread is None:
        e=ARGUMENT_FAULT("add thread(thread) got None parameter")
        e.log()
        raise e
    
    first_page_url=thread.page_url[0]
    gql="SELECT * FROM Thread WHERE page_url = \'%s\'"%str(first_page_url)
    #return gql
    
    #results = db.GqlQuery("SELECT * FROM Thread WHERE page_url = %s"%first_page_url)
    results=db.GqlQuery(gql)
    same_thread=results.get()
    
    #if found,means there is already a thread
    if same_thread is not None:
        #now should refresh the page url list
        e=THREAD_EXIST("thread existed when checking thread with page url:%s"%str(first_page_url))
        e.log()
        #raise e
        
        lst_page_url_db=same_thread.page_url
        lst_page_url_new = thread.page_url
        
        if lst_page_url_db == lst_page_url_new :
            pass
        else:
            same_thread.page_url=thread.page_url
            e=THREAD_PAGE_REFRESH("Thread Exist when to add thread,refresh it!thread_id:%s,page_url:%s,incoming page_url:%s"%\
                                  (same_thread.thread_id,same_thread.page_url,thread.page_url))
            
            same_thread.put()
            e.log()
        return same_thread
    
    else:
        #not found
        
       
        thread.put()
        e=THREAD_ADDED("thread:%s"%str(thread))
        e.log()

        return thread

def add_page_if_now_exist(page):
    q=Page.all()
    q.filter('url =',page.url)
    page_db=q.get()
    
    if page_db is None:
        page.page_id=uuid.uuid1().hex
        page.put()
        return page
    else:
        e=PAGE_EXIST("url:%s"%str(page.url))
        e.log()
        return None
    
def add_page(list_page):
    if list_page is None:
        e=ARGUMENT_FAULT("add add_page(list_page) got None parameter")
        e.log()
        raise e
    added=0
    for page in list_page:
        ret=add_page_if_now_exist(page)
        if ret is not None:
            adde=added+1
        
    return added

        
    
    
    
    

    

        